import os
from building import *

Import('asenv')
MODULES=asenv['MODULES']
ASROOT=asenv['ASROOT']

cwd = GetCurrentDir()

objs = []

if('CAN' in MODULES):
    objs += Glob('mcal/Can.c')
    asenv.Append(CPPDEFINES=['CAN_LL_DL=8'])

if('SCAN' in MODULES):
    objs += Glob('../common/mcal/SCan.c')

if('DIO' in MODULES):
    objs += Glob('mcal/Dio.c')

if('MCU' in MODULES):
    objs += Glob('mcal/Mcu.c')

if('PORT' in MODULES):
    objs += Glob('mcal/Port.c')

if('LWIP' in MODULES):
    objs += Glob('lwip/*.c')
    objs += Glob('Libraries/STM32_ETH_Driver/src/*.c')
    asenv.Append(CPPPATH=['%s/Libraries/STM32_ETH_Driver/inc'%(cwd)])
    asenv.Append(CPPDEFINES=['LWIP_DHCP=1'])

asenv.Append(CPPDEFINES=['USE_STDPERIPH_DRIVER'])
objs += Glob('Libraries/STM32F10x_StdPeriph_Driver/src/*.c')
objs += Glob('Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/*.c')
asenv.Append(CPPPATH=['%s/Libraries/STM32F10x_StdPeriph_Driver/inc'%(cwd)])
asenv.Append(CPPPATH=['%s/Libraries/CMSIS/Include'%(cwd)])
asenv.Append(CPPPATH=['%s/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x'%(cwd)])

asenv.Append(CCFLAGS=['-O0','-g'])
asenv.Append(ASFLAGS=['-g'])

asenv.Append(CCFLAGS=['-mcpu=cortex-m3','-mthumb'])
asenv.Append(ASFLAGS=['-mcpu=cortex-m3','-mthumb'])

asenv.Append(CCFLAGS=['-ffunction-sections','-fdata-sections'])
asenv.Append(LINKFLAGS='--gc-sections')

asenv.Append(LIBS=['c','gcc'])

SelectCompilerArmNoneEabi()

if(asenv['RELEASE']=='asboot'):
    asenv['flsdrv']['objs'] += Glob('mcal/Flash.c')
    asenv['flsdrv']['objs'] += asenv.Object('Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_flash.c')
    asenv['flsdrv']['CPPPATH'] += ['%s/Libraries/STM32F10x_StdPeriph_Driver/inc'%(cwd),
                    '%s/Libraries/CMSIS/Include'%(cwd),
                    '%s/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x'%(cwd)]
    asenv['flsdrv']['CPPDEFINES'] += ['USE_STDPERIPH_DRIVER']
    asenv['flsdrv']['CCFLAGS'] += ['-mcpu=cortex-m3','-mthumb','-ffunction-sections','-fdata-sections']
    asenv['flsdrv']['LINKFLAGS'] += ['--gc-sections']
Return('objs')
